package com.itheima.reggie.controller;

import com.itheima.reggie.common.R;
import com.itheima.reggie.pojo.Setmeal;
import com.itheima.reggie.pojo.ShoppingCart;
import com.itheima.reggie.service.ShoppingCartService;
import com.itheima.reggie.utils.BaseContest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 购物车管理
 */
@RestController
@RequestMapping("shoppingCart")
public class ShoppingCartController {
    @Autowired
    ShoppingCartService shoppingCartService;
    /**
     * 添加购物车
     */
    @PostMapping("add")
    public R<ShoppingCart> add(@RequestBody ShoppingCart shoppingCart){
        //判断购物车里是否存在用户添加的商品
        ShoppingCart one = shoppingCartService.query().eq("user_id", BaseContest.get())
                .eq(shoppingCart.getDishId() != null, "dish_id", shoppingCart.getDishId())
                .eq(shoppingCart.getSetmealId() != null, "setmeal_id", shoppingCart.getSetmealId())
                .one();

        if (one!=null) {
            //判断是否存在 存在添加数量
            one.setNumber(one.getNumber()+1);
            shoppingCartService.updateById(one);
            return R.success(one);
        }else {
            //不存在 创建商品信息
            shoppingCart.setNumber(1);
            shoppingCart.setUserId(BaseContest.get());
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartService.save(shoppingCart);
            return R.success(shoppingCart);
        }
    }
    /**
     * 查询购物车信息
     */
    @GetMapping("list")
    public R<List<ShoppingCart>> list(){
        List<ShoppingCart> user_id = shoppingCartService.query().eq("user_id", BaseContest.get()).list();
        return R.success(user_id);
    }

    /**
     * 删除商品
     * @return
     */
    @PostMapping("sub")
    public R<ShoppingCart> sub(@RequestBody ShoppingCart shoppingCart){
        //查询商品信息
        ShoppingCart one = shoppingCartService.query()
                .eq("user_id",BaseContest.get())
                .eq(shoppingCart.getDishId() != null, "dish_id", shoppingCart.getDishId())
                .eq(shoppingCart.getSetmealId() != null, "setmeal_id", shoppingCart.getSetmealId())
                .one();
        if (one.getNumber()>1) {
            //如果商品数量大于1 把商品数量-1
            one.setNumber(one.getNumber()-1);
            shoppingCartService.updateById(one);
            return R.success(one);
        }else {
            //如果商品数量小于1 直接删除商品
            shoppingCartService.update()
                    .eq("user_id",BaseContest.get())
                    .eq(shoppingCart.getDishId() != null, "dish_id", shoppingCart.getDishId())
                    .eq(shoppingCart.getSetmealId() != null, "setmeal_id", shoppingCart.getSetmealId())
                    .remove();
            return R.success(shoppingCart);
        }
    }
    /**
     * 清空购物车
     */
    @DeleteMapping("clean")
    public R<String> deleteClean(){
        shoppingCartService.update().eq("user_id",BaseContest.get()).remove();
        return R.success("成功");
    }
}
